package com.action;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.base.action.BaseAction;
import com.model.Role;
import com.model.User;
import com.opensymphony.xwork2.ModelDriven;
import com.service.IRoleService;
import com.service.IUserService;
import com.util.PageUtil;

public class UserAction extends BaseAction implements ModelDriven<User> {
	
	private static final long serialVersionUID = -823933714338571576L;
	
	private IUserService userService;
	private IRoleService roleService;
	private User user;
	private int roleID;
	int page;
	HttpServletRequest request;

	public void setRoleID(int roleID) {
		this.roleID = roleID;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	@Override
	public User getModel() {
		return user;
	}
	
	public String getAll(){
		PageUtil pageModel = new PageUtil();
		// 获得当前页
		if ((String) ServletActionContext.getRequest().getParameter("page") == null) {
			pageModel.setPage(1);
		} else {
			page = Integer.parseInt((String) ServletActionContext.getRequest()
					.getParameter("page"));
			if (page != 0) {
				pageModel.setPage(page);
			}
		}

		pageModel.setPageSize(3);// 设置页面显示最大 值
		pageModel.setTotalCount(userService.count("select count(*) from User")); // 数据总条数
		pageModel.setNum(3); // 设置当前页的前后距离
		// 通过当前页和
		List<User> aboutList = (List<User>) userService.getAll("from User", page, 3);
		pageModel.setItems(aboutList);
		request = ServletActionContext.getRequest();
		request.setAttribute("count", aboutList.size());// 放置在request中
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("page", pageModel.getPage());
		return "getAll";
	}
	public String add(){
		Role r=roleService.get(roleID);
		user.setRole(r);
		userService.save(user);
		return "add";
	}
	public String del(){
		Integer id=Integer.parseInt(this.getRequest().getParameter("id"));		
		userService.delete(id);
		return "del";
	}
	public String get(){
		String idStr=this.getRequest().getParameter("id");
		if(idStr!=null && idStr.length()!=0 && !idStr.equals("")){
			Integer id=Integer.parseInt(idStr);		
			User model=userService.get(id);		
			this.getRequest().setAttribute("model", model);						
		}
		List<Role> roles=roleService.getAll();			
		this.getRequest().setAttribute("roles", roles);		
		return "get";
	}
	public String edit(){
		Role r=roleService.get(roleID);
		user.setRole(r);
		userService.update(user);
		return "edit";
	}
	public String multiDel(){
		String[] arr=this.getRequest().getParameterValues("select");
		System.out.println(Arrays.asList(arr));
		for(int i=0;i<arr.length;i++){
			int id=Integer.parseInt(arr[i]);
			userService.delete(id);
		}
		return "multiDel";
	}
	public String login(){
		User u=userService.login(user);
		if(u!=null){
			this.getSession().put("loginUser", u);
			if(!("system".equals(u.getRole().getRolename()))){
				return "loginSuccessToFront";
			}else{
				return "loginSuccessToBack";
			}			
		}else{
			
			return INPUT;
		}
	}
	public String logout(){
		this.getSession().remove("loginUser");
		return INPUT;
	}	
}
